package com.design.android.personnel.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import androidx.annotation.NonNull;
import com.design.android.personnel.bean.User;
import com.design.android.personnel.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 */
public class UserOperate extends BaseDatabaseOperate<User> {
    private static final String TABLE_NAME = "user";

    private static final String[] TABLE_FILED = new String[]{
            "id",
            "phone",
            "password",
            "salt",
            "username",
            "sex",
            "createTime",
            "updateTime",
            "remark"
    };

    public UserOperate(@NonNull Context context) {
        super(context);
    }

    @Override
    public void add(@NonNull User user) {
        String createTime = DateUtils.date2Str(user.getCreateTime());

        ContentValues contentValues = new ContentValues();
        contentValues.put("phone", user.getPhone());
        contentValues.put("password", user.getPassword());
        contentValues.put("salt", user.getSalt());
        contentValues.put("username", user.getUsername());
        contentValues.put("sex", user.getSex());
        contentValues.put("createTime", createTime);
        contentValues.put("updateTime", createTime);
        contentValues.put("remark", user.getRemark());

        if (database == null || !database.isOpen()) {
            open();
        }
        database.insert(TABLE_NAME, null, contentValues);
        database.close();
    }

    @Override
    public void delete(@NonNull Integer id) {
        if (database == null || !database.isOpen()) {
            open();
        }
        database.delete(TABLE_NAME, "id=?", new String[]{String.valueOf(id)});
        database.close();
    }

    @Override
    public void update(@NonNull User user) {

    }

    @Override
    public User select(@NonNull Integer id) {
        if (database == null || !database.isOpen()) {
            open();
        }
        Cursor query = database.query(TABLE_NAME, TABLE_FILED,
                "id=?", new String[]{String.valueOf(id)},
                null, null, null);

        query.moveToFirst();

        User user = new User();
        user.setId(query.getInt(0));
        user.setPhone(query.getString(1));
        user.setPassword(query.getString(2));
        user.setSalt(query.getString(3));
        user.setUsername(query.getString(4));
        user.setSex(query.getString(5));
        user.setCreateTime(DateUtils.str2Date(query.getString(6)));
        user.setUpdateTime(DateUtils.str2Date(query.getString(7)));
        user.setRemark(query.getString(8));

        close();
        return user;
    }

    public User select(@NonNull String phone) {
        User user = null;
        List<User> userList = select("phone=?", new String[]{phone});

        if (userList.size() > 0) {
            user = userList.get(0);
        }

        return user;
    }

    public List<User> select(String selection, String[] selectionArgs) {
        List<User> userList = new ArrayList<>();

        if (database == null || !database.isOpen()) {
            open();
        }
        Cursor query = database.query(TABLE_NAME, TABLE_FILED,
                selection, selectionArgs,
                null, null, "id");

        if (query.getCount() != 0) {
            query.moveToFirst();
            do {
                User user = new User();
                user.setId(query.getInt(0));
                user.setPhone(query.getString(1));
                user.setPassword(query.getString(2));
                user.setSalt(query.getString(3));
                user.setUsername(query.getString(4));
                user.setSex(query.getString(5));
                user.setCreateTime(DateUtils.str2Date(query.getString(6)));
                user.setUpdateTime(DateUtils.str2Date(query.getString(7)));
                user.setRemark(query.getString(8));

                userList.add(user);
            } while (query.moveToNext());

        }
        close();
        return userList;
    }
}
